#!/bin/bash
# ONVIF摄像头监控系统部署脚本

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

# 配置变量
COMPOSE_FILE="docker-compose.yml"
PROJECT_NAME="onvif-monitor"

echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}  ONVIF摄像头监控系统部署脚本${NC}"
echo -e "${BLUE}========================================${NC}"
echo

# 检查Docker和Docker Compose
check_dependencies() {
    echo -e "${YELLOW}检查依赖...${NC}"
    
    if ! command -v docker &> /dev/null; then
        echo -e "${RED}错误: Docker未安装${NC}"
        exit 1
    fi
    
    if ! command -v docker-compose &> /dev/null; then
        echo -e "${RED}错误: Docker Compose未安装${NC}"
        exit 1
    fi
    
    echo -e "${GREEN}✓ Docker和Docker Compose已安装${NC}"
    echo
}

# 创建必要的目录
create_directories() {
    echo -e "${YELLOW}创建必要目录...${NC}"
    
    mkdir -p data logs grafana/dashboards
    
    # 设置权限
    chmod 755 data logs
    
    echo -e "${GREEN}✓ 目录创建完成${NC}"
    echo
}

# 构建镜像
build_images() {
    echo -e "${YELLOW}构建Docker镜像...${NC}"
    
    docker-compose -f $COMPOSE_FILE -p $PROJECT_NAME build onvif-monitor
    
    echo -e "${GREEN}✓ 镜像构建完成${NC}"
    echo
}

# 启动服务
start_services() {
    echo -e "${YELLOW}启动服务...${NC}"
    
    # 启动所有服务
    docker-compose -f $COMPOSE_FILE -p $PROJECT_NAME up -d
    
    echo -e "${GREEN}✓ 服务启动完成${NC}"
    echo
}

# 等待服务就绪
wait_for_services() {
    echo -e "${YELLOW}等待服务就绪...${NC}"
    
    # 等待ONVIF监控服务
    echo "等待ONVIF监控服务..."
    timeout=60
    while [ $timeout -gt 0 ]; do
        if curl -s http://localhost:9098/metrics > /dev/null 2>&1; then
            echo -e "${GREEN}✓ ONVIF监控服务就绪${NC}"
            break
        fi
        sleep 2
        timeout=$((timeout-2))
    done
    
    if [ $timeout -le 0 ]; then
        echo -e "${RED}✗ ONVIF监控服务启动超时${NC}"
    fi
    
    # 等待Prometheus
    echo "等待Prometheus服务..."
    timeout=60
    while [ $timeout -gt 0 ]; do
        if curl -s http://localhost:9091/-/ready > /dev/null 2>&1; then
            echo -e "${GREEN}✓ Prometheus服务就绪${NC}"
            break
        fi
        sleep 2
        timeout=$((timeout-2))
    done
    
    # 等待Grafana
    echo "等待Grafana服务..."
    timeout=60
    while [ $timeout -gt 0 ]; do
        if curl -s http://localhost:3000/api/health > /dev/null 2>&1; then
            echo -e "${GREEN}✓ Grafana服务就绪${NC}"
            break
        fi
        sleep 2
        timeout=$((timeout-2))
    done
    
    echo
}

# 显示服务状态
show_status() {
    echo -e "${YELLOW}服务状态:${NC}"
    docker-compose -f $COMPOSE_FILE -p $PROJECT_NAME ps
    echo
}

# 显示访问信息
show_access_info() {
    echo -e "${GREEN}========================================${NC}"
    echo -e "${GREEN}  部署完成！${NC}"
    echo -e "${GREEN}========================================${NC}"
    echo
    echo -e "${BLUE}服务访问地址:${NC}"
    echo "  📊 Prometheus指标:    http://localhost:9098/metrics"
    echo "  🔍 Prometheus Web UI: http://localhost:9091"
    echo "  📈 Grafana仪表板:     http://localhost:3000 (admin/admin123)"
    echo "  🚨 AlertManager:      http://localhost:9093"
    echo "  📊 Node Exporter:     http://localhost:9100/metrics"
    echo
    echo -e "${BLUE}常用命令:${NC}"
    echo "  查看日志:   docker-compose -p $PROJECT_NAME logs -f [service]"
    echo "  停止服务:   docker-compose -p $PROJECT_NAME down"
    echo "  重启服务:   docker-compose -p $PROJECT_NAME restart [service]"
    echo "  查看状态:   docker-compose -p $PROJECT_NAME ps"
    echo
    echo -e "${BLUE}配置文件:${NC}"
    echo "  Prometheus:   prometheus.yml"
    echo "  AlertManager: alertmanager.yml"
    echo "  告警规则:    alerts/onvif_alerts.yml"
    echo "  应用配置:    config/config.yaml"
    echo
}

# 停止服务
stop_services() {
    echo -e "${YELLOW}停止服务...${NC}"
    docker-compose -f $COMPOSE_FILE -p $PROJECT_NAME down
    echo -e "${GREEN}✓ 服务已停止${NC}"
}

# 清理资源
cleanup() {
    echo -e "${YELLOW}清理资源...${NC}"
    docker-compose -f $COMPOSE_FILE -p $PROJECT_NAME down -v --rmi local
    docker system prune -f
    echo -e "${GREEN}✓ 清理完成${NC}"
}

# 显示帮助信息
show_help() {
    echo "用法: $0 [命令]"
    echo
    echo "命令:"
    echo "  start     - 启动所有服务 (默认)"
    echo "  stop      - 停止所有服务"
    echo "  restart   - 重启所有服务"
    echo "  status    - 显示服务状态"
    echo "  logs      - 显示服务日志"
    echo "  cleanup   - 清理所有资源"
    echo "  help      - 显示此帮助信息"
    echo
}

# 主函数
main() {
    case "${1:-start}" in
        start)
            check_dependencies
            create_directories
            build_images
            start_services
            wait_for_services
            show_status
            show_access_info
            ;;
        stop)
            stop_services
            ;;
        restart)
            stop_services
            sleep 2
            start_services
            wait_for_services
            show_status
            ;;
        status)
            show_status
            ;;
        logs)
            docker-compose -f $COMPOSE_FILE -p $PROJECT_NAME logs -f "${2:-}"
            ;;
        cleanup)
            cleanup
            ;;
        help|--help|-h)
            show_help
            ;;
        *)
            echo -e "${RED}未知命令: $1${NC}"
            echo
            show_help
            exit 1
            ;;
    esac
}

main "$@"
